home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 11243 / 11243.xpi / chrome / skipscreen.jar / content / script-compiler.js < prev    next >
Text File  |  2009-12-14  |  13KB  |  337 lines

  1. var skipscreen_gmCompiler={
  2.  
  3.     // getUrlContents adapted from Greasemonkey Compiler
  4.     // http://www.letitblog.com/code/python/greasemonkey.py.txt
  5.     // used under GPL permission
  6.     //
  7.     // most everything else below based heavily off of Greasemonkey
  8.     // http://greasemonkey.devjavu.com/
  9.     // used under GPL permission
  10.  
  11.     urlIsIncluded: function(href) {
  12.  
  13.     return ( /http:\/\/.*depositfiles\.com\/files\/.*/.test(href)
  14.          || /http:\/\/.*depositfiles\.com\/([a-z]{2})\/files\/.*/.test(href)
  15.          || /http:\/\/.*depositfiles\.com\/auth-.*/.test(href)
  16.          || /http:\/\/www\.divshare\.com\/.*/.test(href)
  17. //         || /http:\/\/www\.filestube\.com\/.*?\/details\.html/.test(href)
  18. //         || /http:\/\/www\.filestube\.com\/[a-z0-9]{20}.*/.test(href)
  19.          || /http:\/\/hotfile\.com\/dl\/.*/.test(href)
  20.          || /http:\/\/www\.hotfile\.com\/dl\/.*/.test(href)
  21.          || /http:\/\/.*limelinx\.com\/.*/.test(href)
  22.          // linkbucks-family is broken-out, below
  23.          || /http:\/\/link-protector\.com\/.*/.test(href)
  24.          || /http:\/\/([a-zA-Z0-9]){6}\.link-protector\.com\//.test(href)
  25.          || /http:\/\/.*mediafire\.com\/\?.*/.test(href)
  26.          || /http:\/\/.*mediafire\.com\/download\.php\?.*/.test(href)
  27.          || /http:\/\/.*mediafire\.com\/file\/.*/.test(href)
  28.          || /http:\/\/.*megashare\.com\/.*/.test(href)
  29.          || /http:\/\/.*megashares\.com\/.*/.test(href)
  30.          || /http:\/\/.*megaupload\.com\/\?.*/.test(href)
  31.          || /http:\/\/.*megaporn\.com\/\?.*/.test(href)
  32.          || /http:\/\/.*rapidshare\.com\/files\/.*/.test(href)
  33.          || /http:\/\/.*rapidshare\.de\/files\/.*/.test(href)
  34.          || /http:\/\/www\.sendspace\.com\/.*/.test(href)
  35.          || /http:\/\/sharebee\.com\/.*/.test(href)
  36.          || /http:\/\/.*zshare\.net\/download\/.*/.test(href)
  37.          || /http:\/\/.*zshare\.net\/audio\/.*/.test(href)
  38.          || /http:\/\/digg\.com\/(.{5}|.{6})$/.test(href)
  39.          || /http:\/\/www\.digg\.com\/(.{5}|.{6})$/.test(href)
  40.          || /http:\/\/uploaded\.to\/file\/.*/.test(href)
  41.          || /http:\/\/uploaded\.to\/\?view/.test(href)
  42.          || /http:\/\/www\.storage\.to\/get\/.*/.test(href)
  43.          || /http:\/\/www\.4shared\.com\/file\/.*/.test(href)
  44.          || /http:\/\/www\.4shared\.com\/get\/.*/.test(href)
  45. // removed for 0.3.200911xx release
  46. //         || /http:\/\/netload.in\/(index.php\?id=10|date).*/.test(href)
  47.  
  48.          // the following are all for the linkbucks family
  49.           || /http:\/\/(([0-9a-z]+)|(www))\.linkbucks\.com\/(link\/[0-9]+)?/.test(href)
  50.          || /http:\/\/(([0-9a-z]+)|(www))\.baberepublic\.com\/(link\/[0-9]+)?/.test(href)
  51.          || /http:\/\/(([0-9a-z]+)|(www))\.blahetc\.com\/(link\/[0-9]+)?/.test(href)
  52.          || /http:\/\/(([0-9a-z]+)|(www))\.linkgalleries\.net\/(link\/[0-9]+)?/.test(href)
  53.          || /http:\/\/(([0-9a-z]+)|(www))\.linkseer\.net\/(link\/[0-9]+)?/.test(href)
  54.          || /http:\/\/(([0-9a-z]+)|(www))\.picturesetc\.net\/(link\/[0-9]+)?/.test(href)
  55.          || /http:\/\/(([0-9a-z]+)|(www))\.placepictures\.com\/(link\/[0-9]+)?/.test(href)
  56.          || /http:\/\/(([0-9a-z]+)|(www))\.qvvo\.com\/(link\/[0-9]+)?/.test(href)
  57.          || /http:\/\/(([0-9a-z]+)|(www))\.realfiles\.net\/(link\/[0-9]+)?/.test(href)
  58.          || /http:\/\/(([0-9a-z]+)|(www))\.seriousfiles\.com\/(link\/[0-9]+)?/.test(href)
  59.          || /http:\/\/(([0-9a-z]+)|(www))\.thatsprime\.com\/(link\/[0-9]+)?/.test(href)
  60.          || /http:\/\/(([0-9a-z]+)|(www))\.thesefiles\.com\/(link\/[0-9]+)?/.test(href)
  61.          || /http:\/\/(([0-9a-z]+)|(www))\.thesegalleries\.com\/(link\/[0-9]+)?/.test(href)
  62.          || /http:\/\/(([0-9a-z]+)|(www))\.thosegalleries\.com\/(link\/[0-9]+)?/.test(href)
  63.          || /http:\/\/(([0-9a-z]+)|(www))\.tinybucks\.net\/(link\/[0-9]+)?/.test(href)
  64.          || /http:\/\/(([0-9a-z]+)|(www))\.uberpicz\.com\/(link\/[0-9]+)?/.test(href)
  65.          || /http:\/\/(([0-9a-z]+)|(www))\.ubervidz\.com\/(link\/[0-9]+)?/.test(href)
  66.          || /http:\/\/(([0-9a-z]+)|(www))\.ubucks\.net\/(link\/[0-9]+)?/.test(href)
  67.          || /http:\/\/(([0-9a-z]+)|(www))\.ugalleries\.net\/(link\/[0-9]+)?/.test(href)
  68.          || /http:\/\/(([0-9a-z]+)|(www))\.urlpulse\.net\/(link\/[0-9]+)?/.test(href)
  69.          || /http:\/\/(([0-9a-z]+)|(www))\.viraldatabase\.com\/(link\/[0-9]+)?/.test(href)
  70.          || /http:\/\/(([0-9a-z]+)|(www))\.youfap\.com\/(link\/[0-9]+)?/.test(href)
  71.          || /http:\/\/(([0-9a-z]+)|(www))\.zxxo\.net\/(link\/[0-9]+)?/.test(href)
  72.  
  73.            ) ;
  74.     },
  75.  
  76.     urlIsExcluded: function(href) {
  77.  
  78.     return ! ( /http:\/\/ad\.yieldmanager\.com\/.*/.test(href)
  79.            || /http:\/\/transition\.adtrgt\.com\/.*/.test(href)
  80.            || /http:\/\/.*adbrite\.com\/.*/.test(href)
  81.            || /http:\/\/www\.addthis\.com\/.*/.test(href)
  82.  
  83.          );
  84.  
  85.     },
  86.  
  87.     urlIsAllowed: function(href) {
  88.  
  89.     return skipscreen_gmCompiler.urlIsIncluded(href)
  90.         && skipscreen_gmCompiler.urlIsExcluded(href);
  91.  
  92.     },
  93.  
  94.     getUrlContents: function(aUrl){
  95.     var    ioService = Components.classes["@mozilla.org/network/io-service;1"]
  96.       .getService(Components.interfaces.nsIIOService);
  97.     var    scriptableStream = Components
  98.       .classes["@mozilla.org/scriptableinputstream;1"]
  99.       .getService(Components.interfaces.nsIScriptableInputStream);
  100.     var unicodeConverter = Components
  101.       .classes["@mozilla.org/intl/scriptableunicodeconverter"]
  102.       .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  103.     unicodeConverter.charset="UTF-8";
  104.  
  105.     try {
  106.         var    channel = ioService.newChannel(aUrl, null, null);
  107.         var    input = channel.open();
  108.         scriptableStream.init(input);
  109.         var    str = scriptableStream.read(input.available());
  110.         scriptableStream.close();
  111.         input.close();
  112.     } catch(e) {
  113.         alert("SkipScreen compiler failed on attempt to include " + aUrl);
  114.     }
  115.  
  116.     try {
  117.         return unicodeConverter.ConvertToUnicode(str);
  118.     } catch (e) {
  119.         return str;
  120.     }
  121.     },
  122.  
  123.     isGreasemonkeyable: function(url) {
  124.     var scheme = Components.classes["@mozilla.org/network/io-service;1"]
  125.     .getService(Components.interfaces.nsIIOService)
  126.     .extractScheme(url);
  127.     return (
  128.         (scheme == "http" || scheme == "https" || scheme == "file") &&
  129.         !/hiddenWindow\.html$/.test(url)
  130.     );
  131.     },
  132.  
  133.     contentLoad: function(e) {
  134.     var unsafeWin = e.target.defaultView;
  135.     if (unsafeWin.wrappedJSObject) unsafeWin = unsafeWin.wrappedJSObject;
  136.  
  137.     var unsafeLoc = new XPCNativeWrapper(unsafeWin, "location").location;
  138.     var href = new XPCNativeWrapper(unsafeLoc, "href").href;
  139.  
  140.     if (skipscreen_gmCompiler.isGreasemonkeyable(href) && skipscreen_gmCompiler.urlIsAllowed(href)) {
  141.         var script = skipscreen_gmCompiler.getUrlContents('chrome://SkipScreen/content/skipscreen.js');
  142.         skipscreen_gmCompiler.injectScript(script, href, unsafeWin);
  143.     }
  144.     },
  145.  
  146.     injectScript: function(script, url, unsafeContentWin) {
  147.     var sandbox, logger, xmlhttpRequester;
  148.     var safeWin = new XPCNativeWrapper(unsafeContentWin);
  149.  
  150.     sandbox = new Components.utils.Sandbox(safeWin);
  151.  
  152.     var storage = new skipscreen_ScriptStorage();
  153.     xmlhttpRequester = new skipscreen_xmlhttpRequester(
  154.         unsafeContentWin, window
  155.     );
  156.  
  157.     sandbox.window = safeWin;
  158.     sandbox.document = sandbox.window.document;
  159.     sandbox.unsafeWindow = unsafeContentWin;
  160.  
  161.     // patch missing properties on xpcnw
  162.     sandbox.XPathResult = Components.interfaces.nsIDOMXPathResult;
  163.  
  164.     // add the GM APIs
  165.     sandbox.GM_addStyle = function(css) { skipscreen_gmCompiler.addStyle(sandbox.document, css); };
  166.     sandbox.GM_setValue = skipscreen_gmCompiler.hitch(storage, "setValue");
  167.     sandbox.GM_getValue = skipscreen_gmCompiler.hitch(storage, "getValue");
  168.     sandbox.GM_openInTab = skipscreen_gmCompiler.hitch(this, "openInTab", unsafeContentWin);
  169.     sandbox.GM_xmlhttpRequest = skipscreen_gmCompiler.hitch(xmlhttpRequester, "contentStartRequest");
  170.  
  171.         // TODO: the following is CURRENTLY unsupported, but would be nice to implement...
  172.     sandbox.GM_log = function(){};
  173.  
  174.     // http://dev.iceburg.net/jquery/jqDnR/
  175.     // Drag-n-Resize for jQuery
  176.     var jDNR = skipscreen_gmCompiler.getUrlContents('chrome://SkipScreen/content/jqDnR.js');
  177.     var jQuery = skipscreen_gmCompiler.getUrlContents('chrome://SkipScreen/content/jquery-1.3.2.min.js');
  178.  
  179.     var jq = "(function(){"+jQuery+"})()";
  180.     var jd = "(function(){"+jDNR+"})()";
  181.  
  182.     // mega-upload file may be swapped based on build-type.....
  183.     var mega = skipscreen_gmCompiler.getUrlContents('chrome://SkipScreen/content/megaupload.js');
  184.  
  185.     sandbox.__proto__ = sandbox.window;
  186.  
  187.     try {
  188.  
  189.         this.evalInSandbox(
  190.         "(function(){"+ jQuery + jDNR + mega + script+"})()",
  191.         url,
  192.         sandbox);
  193.  
  194.         //sandbox.start(); // will this work? -- no!!!
  195.     } catch (e) {
  196.         var e2 = new Error(typeof e=="string" ? e : e.message);
  197.         e2.fileName = script.filename;
  198.         e2.lineNumber = 0;
  199.         alert("SkipScreen " + e2);
  200.     }
  201.     },
  202.  
  203.     evalInSandbox: function(code, codebase, sandbox) {
  204.     if (Components.utils && Components.utils.Sandbox) {
  205.         // DP beta+
  206.         Components.utils.evalInSandbox(code, sandbox);
  207.     } else if (Components.utils && Components.utils.evalInSandbox) {
  208.         // DP alphas
  209.         Components.utils.evalInSandbox(code, codebase, sandbox);
  210.     } else if (Sandbox) {
  211.         // 1.0.x
  212.         evalInSandbox(code, sandbox, codebase);
  213.     } else {
  214.         throw new Error("Could not create sandbox.");
  215.     }
  216.     },
  217.  
  218.     openInTab: function(unsafeContentWin, url) {
  219.     var tabBrowser = getBrowser(), browser, isMyWindow = false;
  220.     for (var i = 0; browser = tabBrowser.browsers[i]; i++)
  221.         if (browser.contentWindow == unsafeContentWin) {
  222.         isMyWindow = true;
  223.         break;
  224.         }
  225.     if (!isMyWindow) return;
  226.  
  227.     var loadInBackground, sendReferrer, referrer = null;
  228.     loadInBackground = tabBrowser.mPrefs.getBoolPref("browser.tabs.loadInBackground");
  229.     sendReferrer = tabBrowser.mPrefs.getIntPref("network.http.sendRefererHeader");
  230.     if (sendReferrer) {
  231.         var ios = Components.classes["@mozilla.org/network/io-service;1"]
  232.         .getService(Components.interfaces.nsIIOService);
  233.         referrer = ios.newURI(content.document.location.href, null, null);
  234.     }
  235.     tabBrowser.loadOneTab(url, referrer, null, null, loadInBackground);
  236.     },
  237.  
  238.     hitch: function(obj, meth) {
  239.     var unsafeTop = new XPCNativeWrapper(unsafeContentWin, "top").top;
  240.  
  241.     for (var i = 0; i < this.browserWindows.length; i++) {
  242.         this.browserWindows[i].openInTab(unsafeTop, url);
  243.     }
  244.     },
  245.  
  246.     apiLeakCheck: function(allowedCaller) {
  247.     var stack = Components.stack;
  248.  
  249.     var leaked = false;
  250.     do {
  251.         if (2==stack.language) {
  252.         if ('chrome'!=stack.filename.substr(0, 6) &&
  253.             allowedCaller!=stack.filename
  254.            ) {
  255.             leaked = true;
  256.             break;
  257.         }
  258.         }
  259.  
  260.         stack = stack.caller;
  261.     } while (stack);
  262.  
  263.     return leaked;
  264.     },
  265.  
  266.     hitch: function(obj, meth) {
  267.     if (!obj[meth]) {
  268.         throw "method '" + meth + "' does not exist on object '" + obj + "'";
  269.     }
  270.  
  271.     var hitchCaller = Components.stack.caller.filename;
  272.     var staticArgs = Array.prototype.splice.call(arguments, 2, arguments.length);
  273.  
  274.     return function() {
  275.         if (skipscreen_gmCompiler.apiLeakCheck(hitchCaller)) {
  276.         return;
  277.         }
  278.  
  279.         // make a copy of staticArgs (don't modify it because it gets reused for
  280.         // every invocation).
  281.         var args = staticArgs.concat();
  282.  
  283.         // add all the new arguments
  284.         for (var i = 0; i < arguments.length; i++) {
  285.         args.push(arguments[i]);
  286.         }
  287.  
  288.         // invoke the original function with the correct this obj and the combined
  289.         // list of static and dynamic arguments.
  290.         return obj[meth].apply(obj, args);
  291.     };
  292.     },
  293.  
  294.     addStyle: function(doc, css) {
  295.     var head, style;
  296.     head = doc.getElementsByTagName('head')[0];
  297.     if (!head) { return; }
  298.     style = doc.createElement('style');
  299.     style.type = 'text/css';
  300.     style.innerHTML = css;
  301.     head.appendChild(style);
  302.     },
  303.  
  304.     onLoad: function() {
  305.     var    appcontent = window.document.getElementById("appcontent");
  306.     if (appcontent && !appcontent.greased_skipscreen_gmCompiler) {
  307.         appcontent.greased_skipscreen_gmCompiler = true;
  308.         appcontent.addEventListener("DOMContentLoaded", skipscreen_gmCompiler.contentLoad, false);
  309.     }
  310.     },
  311.  
  312.     onUnLoad: function() {
  313.     //remove now unnecessary listeners
  314.     window.removeEventListener('load', skipscreen_gmCompiler.onLoad, false);
  315.     window.removeEventListener('unload', skipscreen_gmCompiler.onUnLoad, false);
  316.     window.document.getElementById("appcontent")
  317.     .removeEventListener("DOMContentLoaded", skipscreen_gmCompiler.contentLoad, false);
  318.     }
  319.  
  320. }; //object skipscreen_gmCompiler
  321.  
  322.  
  323. function skipscreen_ScriptStorage() {
  324.     this.prefMan = new skipscreen_PrefManager();
  325. }
  326.  
  327. skipscreen_ScriptStorage.prototype.setValue = function(name, val) {
  328.   this.prefMan.setValue(name, val);
  329. };
  330.  
  331. skipscreen_ScriptStorage.prototype.getValue = function(name, defVal) {
  332.     return this.prefMan.getValue(name, defVal);
  333. };
  334.  
  335.  
  336. window.addEventListener('load', skipscreen_gmCompiler.onLoad, false);
  337. window.addEventListener('unload', skipscreen_gmCompiler.onUnLoad, false);